<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="欢迎进入风动社区"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view v-if="info.score < 1" class="">
				<view class="title">
					事件已完成
				</view>
				<view class="msg">
					<text>请对本次服务做出评价吧</text>
				</view>
				<view class="evaluate">
					<l-starRate v-model="star" :disabled="false"></l-starRate>
				</view>
				<view class="tags">
					<view v-for="(item, index) in tagList" :key="index" class="tag-item" @click="tagClick(item)">
						<text :class="item.select == 2 ? 'active' : ''">{{item.title}}</text>
					</view>
				</view>
				<view class="description">
					<u--textarea v-model="description" placeholder="写其他意见和建议" height="120" maxlength="100" count></u--textarea>
				</view>
				<view class="msg-tag">
					满意的话请打五星奥，谢谢您的支持！
				</view>
				<view class="btn" @click="btnSave">
					确认提交
				</view>
			</view>
			<view v-else class="successs">
				<view class="title">
					您已完成评价
				</view>
				<view class="evaluate">
					<l-starRate v-model="info.score" :disabled="false"></l-starRate>
				</view>
				<view class="btn" @click="btnClick">
					返回主页
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadding: true,
				info: {},
				id: '',
				star: 5,
				userinfo: {},
				description: '',
				tags: [],
				tagList: [],
				is_true: true
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('userinfo');
			if (e.id) {
				this.id = e.id
			}
			this.getService()
		},
		methods: {
			// 获取评价信息
			async getService() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'v1/getService',
					data: {
						id: this.id
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				this.loadding = false
				if (res.code == 1) {
					this.info = res.data
					if (res.data.score <= 0) {
						this.loadData()
					}
				}
			},
			// 加载服务类型
			async loadData() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'v1/getQuestion',
					data: {
						type: 'tip'
					},
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					res.data.map(item => {
						item.select = 1
					})
					this.tagList = res.data
				}
			},
			// 选择标签
			tagClick(item) {
				var index = this.tags.indexOf(item.id);
				if (item.select == 1) {
					item.select = 2
					if (index <= -1) {
						this.tags.push(item.id)
					}
				} else {
					item.select = 1
					if (index > -1) {
						this.tags.splice(index, 1);
					}
				}
			},
			// 确认提交
			btnSave() {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确定提交吗？',
					success: function(res) {
						if (res.confirm) {
							that.btnSaveFun()
						}
					}
				});
			},
			// 提交申领 - 方法
			async btnSaveFun() {
				var that = this
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'v1/saveScore',
						data: {
							id: this.id,
							tip: this.tags,
							score: this.star,
							remark: this.description
						},
						header: {
							'token': this.userinfo.token
						}
					});
					this.is_true = true
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/index/index'
							})
						},1500)
					}
				}
			},
			// 返回主页
			btnClick() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.all-main {
		.main {
			.title {
				margin-top: 50rpx;
				font-size: 40rpx;
				font-weight: bold;
				text-align: center;
			}

			.msg {
				margin-top: 10rpx;
				font-size: 28rpx;
				text-align: center;
				color: #c8c9cc;

				text {
					padding-bottom: 10rpx;
					border-bottom: 1px dashed #c8c9cc;
				}
			}

			.evaluate {
				margin-top: 30rpx;
				display: flex;
				justify-content: center;
			}

			.tags {
				display: flex;
				flex-wrap: wrap;
				padding: 30rpx;
				
				.tag-item {
					width: 33%;
					text-align: center;
					padding: 20rpx 0;
					
					text {
						border-radius: 20rpx;
						background-color: #fdf6ec;
						color: #ff9900;
						font-size: 28rpx;
						padding: 10rpx 20rpx;
					}
					
					.active {
						background-color: #ff9900;
						color: #ffffff;
					}
				}
			}
			
			.description {
				padding: 30rpx 60rpx;
			}
			
			.msg-tag {
				margin-top: 10rpx;
				text-align: center;
				font-size: 26rpx;
				color: #909399;
			}
			
			.btn {
				margin: 40rpx;
				margin-top: 80rpx;
				text-align: center;
				background-color: #ff9900;
				color: #ffffff;
				padding: 30rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>